package com.tecent.chat.actvity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.tecent.chat.R;
import com.tecent.chat.po.User;

import org.litepal.LitePal;

public class SQLActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sql);

        Button btn1 = findViewById(R.id.lite1);
        setClick1(btn1);

        Button btn2 = findViewById(R.id.lite2);
        setClick2(btn2);

        Button btn3 = findViewById(R.id.lite3);
        setClick3(btn3);

        Button btn4 = findViewById(R.id.lite4);
        setClick4(btn4);

    }


    public void setClick1 (Button btn){
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = LitePal.getDatabase();

                Toast.makeText(SQLActivity.this, "click", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void setClick2 (Button btn){
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User u = new User();
                u.setId(1);
                u.setName("huahua");
                u.save();

                Toast.makeText(SQLActivity.this, "click", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void setClick3 (Button btn){
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User u = LitePal.find(User.class, 1);
                System.out.println(u);
                if(u != null){
                    u.setName("lala");
                    u.save();
                    Toast.makeText(SQLActivity.this, "update ok", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(SQLActivity.this, "the data not exists", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void setClick4 (Button btn){
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LitePal.delete(User.class, 1);
                Toast.makeText(SQLActivity.this, "delete ok", Toast.LENGTH_SHORT).show();
            }
        });
    }

}
